﻿using Microsoft.Extensions.Options;
using Yz.Base;
using Yz.Base.Config;
using Yz.Base.ParaModels;
using Yz.Core.Apis;
using Yz.Core.Base;
using Yz.Core.Extensions;
using Yz.DataAccess.ApiAccesses.Interface;

namespace Yz.DataAccess.ApiAccesses
{
    public class ApiFileServiceAccess : IApiFileServiceAccess, IDependency
    {
        private readonly FileServiceConfig _fileServiceConfig;
        public ApiFileServiceAccess(IOptions<FileServiceConfig> fileServiceConfigOps)
        {
            _fileServiceConfig = fileServiceConfigOps.Value;
        }
        /// <summary>
        /// 上传文件
        /// </summary>
        /// <param name="fileServiceConfig"></param>
        /// <param name="apiFile"></param>
        /// <returns></returns>
        public async Task<VmUpFile> UploadFile(ApiFile apiFile, List<ParaThFile> paras = null)
        {
            var headers = GetHeaders(_fileServiceConfig);
            var resp = await RestSharpApi.PostFileAsync(_fileServiceConfig.Url, "/File/Upload", apiFile, paras, headers);
            return resp.Content.ToJsonObject<VmUpFile>();
        }
        /// <summary>
        /// 分片上传图片
        /// </summary>
        /// <param name="fileServiceConfig"></param>
        /// <param name="apiFile"></param>
        /// <param name="paras"></param>
        /// <returns></returns>
        public async Task<VmUpFile> UploadChunkFile(ApiFile apiFile, ParaChunkFile paras)
        {
            var headers = GetHeaders(_fileServiceConfig);
            var resp = await RestSharpApi.PostFileAsync(_fileServiceConfig.Url, "/File/UploadChunk", apiFile, paras, headers);
            return resp.Content.ToJsonObject<VmUpFile>();
        }
        /// <summary>
        /// 合并文件
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        public async Task<VmUpFile> MergeUploadFile(ParaMergeFile paras)
        {
            var headers = GetHeaders(_fileServiceConfig);
            var resp = await RestSharpApi.PostResponseAsync(_fileServiceConfig.Url, "/File/Merge", paras, headers);
            return resp.Content.ToJsonObject<VmUpFile>();
        }

        /// <summary>
        /// 删除文件
        /// </summary>
        /// <param name="paras"></param>
        /// <returns></returns>
        public async Task<VmUpFileBase> DeleteFile(ParaFileId paras)
        {
            var headers = GetHeaders(_fileServiceConfig);
            var resp = await RestSharpApi.PostResponseAsync(_fileServiceConfig.Url, "/File/Delete", paras, headers);
            return resp.Content.ToJsonObject<VmUpFileBase>();
        }

        /// <summary>
        /// 获得接口授权信息
        /// </summary>
        /// <param name="fileServiceConfig"></param>
        /// <returns></returns>
        private List<KeyValuePair<string, string>> GetHeaders(FileServiceConfig fileServiceConfig)
        {
            string appId = fileServiceConfig.AppId;
            string token = (fileServiceConfig.AppSecret + DateTime.Now.ToString(DateFormat.yMdH)).ToMd5Hash();
            Dictionary<string, string> dics = new Dictionary<string, string>() {
                { "appId", appId},
                  { "token", token}
            };
            return dics.ToList();
        }
    }
}
